/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.jy.springbootcmsservice.modules.sys.entity;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.jy.springbootcmsservice.common.base.entity.TreeEntity;
import lombok.Data;

import java.util.List;

/**
 * 菜单Entity
 * @author tm
 * @version 2020-11-23
 */
@Data
public class Menu extends TreeEntity<Menu> {

    /**
     * 名称
     */
	private String name;
    /**
     * 链接
     */
	private String href;
    /**
     * 目标（ mainFrame、_blank、_self、_parent、_top）
     */
	private String target;
    /**
     * 图标
     */
	private String icon;
    /**
     * 是否在菜单中显示（1：显示；0：不显示）
     */
	private String isShow;
    /**
     * 权限标识
     */
	private String permission;

	private String userId;

	public Menu(){
		super();
		this.sort = 30;
		this.isShow = "1";
	}

	public Menu(String id){
		super(id);
	}

	@JsonIgnore
	public static void sortList(List<Menu> list, List<Menu> sourcelist, String parentId, boolean cascade){
		for (int i=0; i<sourcelist.size(); i++){
			Menu e = sourcelist.get(i);
			if (e.getParent()!=null && e.getParent().getId()!=null
					&& e.getParent().getId().equals(parentId)){
				list.add(e);
				if (cascade){
					// 判断是否还有子节点, 有则继续获取子节点
					for (int j=0; j<sourcelist.size(); j++){
						Menu child = sourcelist.get(j);
						if (child.getParent()!=null && child.getParent().getId()!=null
								&& child.getParent().getId().equals(e.getId())){
							sortList(list, sourcelist, e.getId(), true);
							break;
						}
					}
				}
			}
		}
	}

	@JsonIgnore
	public static String getRootId(){
		return "1";
	}

	@Override
	public Menu getParent() {
		return super.parent;
	}

	@Override
	public void setParent(Menu parent) {
		super.parent = parent;
	}

}